﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CP.Controllers.GPM;
using CP.Entities;
using CP.Utility;
using CP.Win.IViews.GPM;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace GPM
{
    public partial class FrmUserInfo : BaseForm, IUserInfoView
    {
        public GridCheckMarksSelection selection;
        public UserInfoController Controller { get; private set; }

        public FrmUserInfo()
        {
            InitializeComponent();
            this.BarComponents = components;
            this.Controller = new UserInfoController(this);
        }

        private void FrmUserInfo_Load(object sender, EventArgs e)
        {
            selection = new GridCheckMarksSelection(this.gvUserInfo);
            this.Controller.Initialize();
        }

        #region IUserInfoView 成员

        public event QueryHandler QuerySubmit;

        public event DeleteUserHandler DeleteSubmit;

        public void BindGroupID(IList<User_Group> groups)
        {
            this.lookupGroupID.DataSource = groups;
        }

        public void BindDeptID(IList<Department> depts)
        {
            //this.repositoryItemSearchLookUpEdit1.DataSource = depts;
            this.lookupDeptID.DataSource = depts;
        }

        public void BindUserInfo(IList<User_Info> users)
        {
            selection.ClearSelection();
            this.gcUserInfo.DataSource = users;
        }

        #endregion

        private void barQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (null != QuerySubmit)
                QuerySubmit();
        }

        private void barDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (0 == selection.SelectedCount)
            {
                ExMessageBox.Show("提示信息：请选中要删除的记录！", AppSetter.SoftName);
                return;
            }

            if (ExMessageBox.Confirm("提示信息：确定要删除选中的记录吗？", AppSetter.SoftName) == DialogResult.No)
                return;

            if (null != DeleteSubmit)
            {
                IList<string> userids = new List<string>();
                foreach (object o in selection.Selection)
                {
                    User_Info user = o as User_Info;
                    if (null != user)
                        userids.Add(user.user_id);
                }
                try
                {
                    DeleteSubmit(userids);
                    LogHelper.Log("用户管理", "delete", "删除", "userids:" + string.Join(",", userids));
                    ExMessageBox.Show("提示信息：删除成功！", AppSetter.SoftName);
                }
                catch (Exception ex)
                {
                    ExMessageBox.Show(ex.Message, AppSetter.SoftName);
                }
            }
        }

        private void barAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmEditUserInfo frmedit = new FrmEditUserInfo();
            frmedit.ShowDialog(this);
        }

        private void barEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (selection.SelectedCount != 1)
            {
                ExMessageBox.Show("提示信息：请选中一条记录编辑", AppSetter.SoftName);
                return;
            }
            User_Info info = selection.Selection[0] as User_Info;
            if (null == info)
            {
                ExMessageBox.Show("提示信息：选中的记录不存在", AppSetter.SoftName);
                return;
            }
            FrmEditUserInfo frmedit = new FrmEditUserInfo("1", info.user_id);
            frmedit.ShowDialog(this);
        }

        private void barGroup_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (selection.SelectedCount != 1)
            {
                ExMessageBox.Show("提示信息：请选中一条用户记录，设定角色", AppSetter.SoftName);
                return;
            }

            User_Info user = selection.Selection[0] as User_Info;
            if (null == user)
            {
                ExMessageBox.Show("提示信息：选中的记录不存在", AppSetter.SoftName);
                return;
            }

            FrmSetUserRole frm = new FrmSetUserRole(user);
            frm.ShowDialog(this);
        }

        private void gvUserInfo_DoubleClick(object sender, EventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hitInfo = view.CalcHitInfo(p);
            if ((hitInfo.HitTest == GridHitTest.Row) ||
                (hitInfo.HitTest == GridHitTest.RowCell) ||
                (hitInfo.HitTest == GridHitTest.RowIndicator))
            {
                User_Info user = gvUserInfo.GetFocusedRow() as User_Info;
                if (null == user)
                    return;

                FrmSetUserRole frm = new FrmSetUserRole(user);
                frm.ShowDialog(this);
            }
        }
        private Point p = new Point();
        private void gvUserInfo_MouseDown(object sender, MouseEventArgs e)
        {
            p.X = e.X;
            p.Y = e.Y;
        }
    }
}
